package com.example.mp.metaObjectHandler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

/**
 * 自定义字段自动填充
 * @auther liujb
 * @date 2021/7/24-22:20
 */
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取到需要被填充的字段的值
        Object lastName = this.getFieldValByName("lastName", metaObject);
        if (null == lastName || "".equals(lastName)){
            this.setFieldValByName("lastName", "Tom", metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取到需要被填充的字段的值
        Object lastName = this.getFieldValByName("lastName", metaObject);
        if (null == lastName || "".equals(lastName)){
            this.setFieldValByName("lastName", "liujb", metaObject);
        }
    }
}
